package datamanagement;

import healthsolution.wk.R;
import healthsolutions.wk.HowToUseHs;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;


public class LeerDatos extends Activity  implements Runnable
{
	private static final String TAG = "LeerDatos activity";
	private Handler threadHandler_ld = new Handler() {};
	
	public Thread Thread_ld = new Thread(this);
	//private Handler handler = new Handler();
	public String strUserType=null;
	public LeerDatos()
	{
		
	}
		
	public void run() {	
		Log.i(TAG, "[run] Run");									//DEBUG
		try
		{
			//threadHandler.postDelayed(this,1000);			// PARA HACER QUE EL HANDLER SEA CONSTANTE
			//threadHandler.sendEmptyMessage(0);			//FD PARA USAR HANDLER CON MENSAJES       	
        	
		}catch(Exception Ex)
		{
			Log.e(TAG, "[run] Exception Error: " +Ex.getMessage());									//DEBUG
		}
		    
	}
	

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		SharedPreferences settings = getSharedPreferences("HS",MODE_PRIVATE);			   
		strUserType=settings.getString("UserKind",null);
		
		Log.i(TAG, "[onCreate] onCreate");		
		 super.onCreate(savedInstanceState);	
		 
		 boSetView();
		 
		 try
		 {
			 voStartListenerButtons();
			
		 }catch(Exception ex)
		 {
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getMessage());			 
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getCause());
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getLocalizedMessage());
		 }
		 try
		 {
			 Thread_ld.start();
			 threadHandler_ld.postDelayed(this,1000);
			 	 
		 }catch(Exception ex)
		 {
			 Log.e(TAG, "[onCreate] Exception Thread: "+ex.getMessage());			 
		 }	
		 
	}
	
	public void voStartListenerButtons()
	{
				
		try
		{
			LinearLayout loLinearEscrituraDatos = (LinearLayout)findViewById(R.id.linearLayoutbt_dl);
        	ImageButton btLeerDatos = (ImageButton) findViewById(R.id.iconBtnLeerDatos_dl);    	
        	ImageButton btEscribirData = (ImageButton) findViewById(R.id.iconBtnEscribir_dl);	//PARA RETIRARLO SI ES USUARIO PARTICULAR   		        	
        	ImageButton btOrdenMedica = (ImageButton) findViewById(R.id.iconBtnOrdenMed_dl);
    		
        	/////////////////////////////////////////////////////////////////////////////////////////////
    		btLeerDatos.setOnClickListener(new OnClickListener()
    	    {     
    			public void onClick(View v) 
    			{				
    				Log.i(TAG,"[voStartListenerButtons] Dentro OnClick LeerDatos");
    				
    			    
    				Intent IntLeerDatos = null;
				    Log.i(TAG,"[voStartListenerButtons] Voy a empezar activity LeerDatos. ");				    
				    try
				    {
				    	IntLeerDatos=new Intent(getApplicationContext(),LeerDatos.class);
				    }catch(Exception Ex)
				    {
				    	Log.e(TAG,"[voStartListenerButtons] Exception LeerDatos Intent: "+Ex);	
				    }
				    try
					{
				    	try
				    	{
				    		threadHandler_ld.removeCallbacks(Thread_ld);					    						    	
				    		threadHandler_ld.removeCallbacksAndMessages(UserChoser.class);
				    		Thread_ld.interrupt();
	        				if(Thread.currentThread()==Thread_ld)
				    		{
	        					Thread_ld.stop();	
				    		}
				    	}catch(Exception ex)
						{
				    		Log.e(TAG,"[voStartListenerButtons] Exception LeerDatos StopThread: "+ex);
						}
				    	try
				    	{
				    		startActivity(IntLeerDatos);
				    	}catch(Exception ex)
						{
				    		Log.e(TAG,"[voStartListenerButtons] Exception StartActivity: "+ex);
						}
					}catch(Exception ex)
					{
						Log.e(TAG,"[voStartListenerButtons] Exception LeerDatos Start: "+ex);	
					}
    			}
    	    });
    		
        	/////////////////////////////////////////////////////////////////////////////////////////////
    	    try
    	    {
    	    	if(strUserType.compareTo("P")==0)
    	    	{
    	    		loLinearEscrituraDatos.removeView(btEscribirData);    	    		
    	    	}else
    	    	{
    	    		btEscribirData.setOnClickListener(new OnClickListener()	        		
	        	    {     
	        			public void onClick(View v) 
	        			{				
	        				Log.i(TAG,"[voStartListenerButtons] Dentro OnClick Escribir Datos");
	        				
	        				try
					    	{
					    		threadHandler_ld.removeCallbacks(Thread_ld);					    						    	
					    		threadHandler_ld.removeCallbacksAndMessages(UserChoser.class);
					    		Thread_ld.interrupt();
		        				if(Thread.currentThread()==Thread_ld)
					    		{
		        					Thread_ld.stop();	
					    		}
					    	}catch(Exception ex)
							{
					    		Log.e(TAG,"[voStartListenerButtons] Exception Escribir StopThread: "+ex);
							}	
	        			}
	        	    });	
    	    	}
    	    	
			}catch(Exception ex)
			{
				Log.e(TAG,"[voStartListenerButtons] EscribirDatos ERROR EXCEPTION: "+ex);
			}  	    		
    		
        	/////////////////////////////////////////////////////////////////////////////////////////////
    		btOrdenMedica.setOnClickListener(new OnClickListener()
    	    {     
    			public void onClick(View v) 
    			{				
    				Log.i(TAG,"[voStartListenerButtons] Dentro OnClick Orden Medica");
    				
    				try
			    	{
			    		threadHandler_ld.removeCallbacks(Thread_ld);					    						    	
			    		threadHandler_ld.removeCallbacksAndMessages(UserChoser.class);
			    		Thread_ld.interrupt();
        				if(Thread.currentThread()==Thread_ld)
			    		{
        					Thread_ld.stop();	
			    		}
			    	}catch(Exception ex)
					{
			    		Log.e(TAG,"[voStartListenerButtons] Exception Orden StopThread: "+ex);
					}			
    			}
    	    });

		}catch(Exception ex)
		{
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex.getMessage());
		}
		
		/////////////////////////////////////// MINIMIZAR ///////////////////////////////////
		ImageButton btMinimizes = (ImageButton) findViewById(R.id.iconminimize_dl);    	    
    	btMinimizes.setOnClickListener(new OnClickListener()
	    {     
			public void onClick(View v) 
			{				
				voMinimizarApp();    				
			}
	    });
	}
	
	///////////////////////////////////////////////////// Set View //////////////////////////////
	public boolean boSetView()
	{
		// SharedPreferences settings = getSharedPreferences("HS",MODE_PRIVATE);
     	Log.i(TAG, "[boSetView] Set ContentView");
		setContentView(R.layout.pudatosleidos);
		
		return true;
	}

	///////////////////////////////////////////onRestart///////////////////////////////////////////////////////////
	 @Override
	 protected void onRestart() {
	     super.onRestart();  // Always call the superclass method first
	     
	 	Intent myIntentRestart=null;
    	try
       	{
    		//myIntent = new Intent(cont, HTService.class);
    		
    		 myIntentRestart=new Intent(this.getApplicationContext(),HowToUseHs.class);
	    		
	       	}catch(Exception ex)
	         {
	        	 Log.e(TAG,"[onCreate] ERROR I EX: "+ex);		        
	         }	    	
	       	try
	       	{
	       		startActivity(myIntentRestart);
	         }catch(Exception ex)
	         {
	        	 Log.e(TAG,"[onCreate] ERROR A EX: "+ex);
	         }
	     // Activity being restarted from stopped state    
	 }
	 
		
	
	
	///////////////////////////////////////////////////////////////////////////////////////////
	public IBinder onBind(Intent arg0)
	{
	    return null;
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////
	// PARA QUE NO PUEDA REGRESAR A LA PANTALLA ANTERIOR
	@Override
	public void onBackPressed() {
		Log.i(TAG, "[onBackPressed] onBackPressed");
	   voMinimizarApp();
	}
	
	public void voMinimizarApp()
	{
		try
		{
			Intent startMain = new Intent(Intent.ACTION_MAIN);
	    	startMain.addCategory(Intent.CATEGORY_HOME);
	    	startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    	startActivity(startMain);
	    	
		}catch(Exception ex)
		{
			Log.i(TAG, "[voMinimizarApp] Exception: "+ex);
		}
		
	}
}
